package com.app.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.app.model.Article;
import com.app.page.CommonPage;
import com.app.request.ArticleRequest;
import com.app.request.PageParamRequest;
import com.app.response.ArticleResponse;
import com.app.result.CommonResult;
import com.app.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 文章管理 前端控制器
 * </p>
 *
 * @author ghoul
 * @since 2024-12-17
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    /**
     * 列表
     *
     * @param request
     * @param pageParamRequest
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ArticleResponse>> getList(@Validated ArticleRequest request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(articleService.getAdminList(request, pageParamRequest)));
    }

    /**
     * 列表
     *
     * @param request
     * @param pageParamRequest
     * @return
     */
    @RequestMapping(value = "/small/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ArticleResponse>> getSmallList(@Validated ArticleRequest request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(articleService.getSmallList(request, pageParamRequest)));
    }

    /**
     * 新增
     *
     * @param articleRequest
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated Article articleRequest) {
        if (articleService.create(articleRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(Integer id) {
        if (articleService.deleteById(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<ArticleResponse> info(Integer id) {
        return CommonResult.success(articleService.getDetail(id));
    }

    /**
     * 修改状态
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.GET)
    public CommonResult<String> updateStatus(Integer id, boolean status) {
        if (articleService.updateStatus(id,status)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     *反映须知
     */
    @RequestMapping(value = "/instructions", method = RequestMethod.GET)
    public CommonResult<Article> instructions() {
        return CommonResult.success(articleService.instructions());
    }
}

